package spring.webflux.service;

import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import spring.webflux.entity.Personnel;

import java.util.HashMap;
import java.util.Map;

@Service
public class PersonnelServiceImpl implements PersonnelService {

    private final Map<Integer, Personnel> personnelMap = new HashMap<>();

    public PersonnelServiceImpl() {
        personnelMap.put(1, new Personnel("zhangsan", "man", 28));
        personnelMap.put(2, new Personnel("lisi", "women", 28));
    }

    @Override
    public Mono<Personnel> getPersonnelById(int id) {
        return Mono.justOrEmpty(personnelMap.get(id));
    }

    @Override
    public Flux<Personnel> getAllPersonnel() {
        return Flux.fromIterable(personnelMap.values());
    }

    @Override
    public Mono<Void> insertPersonnel(Mono<Personnel> personnel) {
        return personnel.doOnNext(p -> {
            personnelMap.put(personnelMap.size() + 1, p);
        }).thenEmpty(Mono.empty());
    }
}
